using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;

namespace Q.Framework.WebControls
{
    public class ComplexBoundField : BoundField
    {
        protected override object GetValue(System.Web.UI.Control controlContainer)
        {
            object component = null;

            string dataField = this.DataField;

            if (controlContainer == null)
            {

                throw new HttpException("DataControlField_NoContainer");

            }

            component = DataBinder.GetDataItem(controlContainer);

            if ((component == null) && !base.DesignMode)
                throw new HttpException("DataItem_Not_Found");

            return GetComplexValue(dataField, component);
        }

        private object GetComplexValue(String dataField, object value)
        {
            String[] aux = dataField.Split('.');
            object val = value.GetType().GetProperty(aux[0]).GetValue(value, null);
            if (aux.Length == 1 || val == null)
                return val;
            List<String> aux2 = aux.ToList();
            aux2.RemoveAt(0);
            return GetComplexValue(String.Join(".",aux2), val);
        }
    }
}